package com.qimou.iems.controller;

import com.qimou.iems.entity.Department;
import com.qimou.iems.entity.PageArgs;
import com.qimou.iems.entity.PageList;
import com.qimou.iems.service.IDepartmentService;
import com.qimou.iems.util.CommonUtil;
import com.qimou.iems.util.StatusConstant;
import com.qimou.iems.util.ViewData;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Qimou Xie
 * @date 2018/3/17 17:29.
 **/

@RestController
@RequestMapping(value = "/department")
public class DepartmentController extends BaseController {

    @Resource
    private IDepartmentService departmentService;


    @RequestMapping(value = "/save")
    public ViewData save(Department department){
        if(CommonUtil.isEmpty(department.getCompanyId(),department.getDepartmentName())){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"参数错误");
        }
        departmentService.save(department);
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


    @RequestMapping(value = "/getDepartmentByCompany")
    public ViewData getDepartmentByCompany(Integer companyId){
        if(CommonUtil.isEmpty(companyId)){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"参数错误");
        }
        return buildSuccessJson(StatusConstant.SUCCESS_CODE,"操作成功",
                departmentService.getDepartmentByCompany(companyId));
    }

    @RequestMapping(value = "/update")
    public ViewData update(Department department){
        if(CommonUtil.isEmpty(department.getCompanyId(),department.getDepartmentName(),
                department.getId())){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"参数错误");
        }
        departmentService.update(department);
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


    @RequestMapping(value = "/list")
    public ViewData list(PageArgs pageArgs,Integer companyId,String departmentName){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("companyId",companyId);
        map.put("departmentName",departmentName);
        PageList<Department> page = departmentService.getDepartmentList(pageArgs, map);
        return buildSuccessViewDataPage(StatusConstant.SUCCESS_CODE,"获取成功",
                page.getTotalSize(),page.getList());
    }





}
